package edu.uoc.tds.pac4.client.ss1;

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Rectangle;

import java.awt.event.*;
import java.rmi.RemoteException;

import edu.uoc.tds.pac4.beans.Centre;
import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.client.ClientInterface.TipusPantalla;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;

public class PantallaModificacioCentres extends PantallaCentres implements PanelClientInterface {
	private static final long serialVersionUID = 1L;	
	private JPanel jPanelContainer = null;
	private JPanel jPanel = null;
	private Client client;
	private Centre centre;
	private JPanelDadesCentre jPanelCentre = null;
	private JButton jButtonGuardar = null;
	private JButton jButtonCancelar = null;

	public PantallaModificacioCentres(){
		super();
		initialize();
	}

	public PantallaModificacioCentres(Client client, RemoteInterface remot, Centre centre)
	{	
		super();
		this.client = client;
		this.remot = remot;
		this.centre = centre;
		initialize();
	}

	private void initialize()
	{		
		this.setTitle(TDSLanguageUtils.getMessage("Client.PantallaModificacioCentre.Title"));
		this.setContentPane(getJPanelContainer(new Dimension(800, 300)));
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		// Tamany del JDialog
		this.setSize(800,600);
		this.setLocationRelativeTo(this);
		this.setVisible(true);
		this.pack();

	}
	/**
	 * This method initializes jPanelContainer	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.ipadx = 800;
			gridBagConstraints.ipady = 300;
			gridBagConstraints.gridy = 0;
			jPanelContainer = new JPanel();
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.add(getJPanel(), gridBagConstraints);

			jPanelContainer.setSize(new Dimension(800, 300));
			//jPanelContainer.setSize(dimension);
		}
		return jPanelContainer;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setBounds(new Rectangle(0, 0, 800, 300));
			jPanel.setLayout(null);
			jPanel.add(getjPanelCentre(), null);			
			jPanel.add(getJButtonGuardar(), null);
			jPanel.add(getJButtonCancelar(), null);
			
		}
		return jPanel;
	}

	/**
	 * This method initializes jButtonGuardar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGuardar() {

		if (jButtonGuardar == null) {
			jButtonGuardar = new JButton("Guardar");
			jButtonGuardar.setBounds(new Rectangle(85, 250, 100, 25));
			jButtonGuardar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						guardar();
						dispose();

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonGuardar;
	}

	private void guardar (){
		centre = jPanelCentre.GetCentre();
		if(centre != null)
			try {
				remot.insertUpdateCentre(centre, false);
				new PantallaError("Info_guardat", PantallaError.Tipus.INFO);    
				client.carregaPantalla(TipusPantalla.ModifBaixaCentres);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (GestioEventsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton("Cancelar");
			jButtonCancelar.setBounds(new Rectangle(240, 250, 100, 25));
			jButtonCancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					setVisible(true);
					dispose();
				}
			});
		}
		return jButtonCancelar;
	}	
	
	/**
	 * This method initializes jPanelUsuari
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getjPanelCentre() {
		if (jPanelCentre == null) {
			jPanelCentre = new JPanelDadesCentre(this);
			jPanelCentre.setBounds(new Rectangle(0, 0, 600, 240));
			jPanelCentre.SetCentre(centre);
		}
		return jPanelCentre;
	}
}